<?php
/*
Primary Author: Derek Shue
Secondary: Alex Arey
Documentation: Cheng Yi
Updated: 12/10/2011
Module Description: The Application References module adds/finds/deletes Application References from the database.
*/
class Application_Model_DbTable_ApplicationReferences extends Zend_Db_Table_Abstract
{

    protected $_name = 'APPLICATIONREFERENCES';


public function getApplicationReferences($where)
    {
        $rows = $this->fetchAll($where);
        if (!$rows) {
            throw new Exception("Could not find row where $where");
        }
        return $rows->toArray();
    }

	public function nextKey(){
		$sqlquery = $this->select()->from($this->_name, array("MAX(REFID)"));
		$insertrow = $this->fetchRow($sqlquery)->toArray(); 
		return $insertrow["MAX(REFID)"] + 1;
	}

    public function addApplicationReference($relationship,$appid,$name,$phone,$address1,$address2,$city,$state,$zip)
    {
        $data = array(
			'RELATIONSHIP'=> $relationship,
			'REFID'=> $this->nextKey(),
			'APPID'=> $appid,
			'NAME'=> $name,
			'PHONE'=> $phone,
			'ADDRESS1'=> $address1,
			'ADDRESS2'=> $address2,
			'CITY'=> $city,
			'STATE'=> $state,
			'ZIP'=> $zip
        );
        $this->insert($data);
    }

    public function updateApplicationReference($refid,$relationship,$appid,$name,$phone,$address1,$address2,$city,$state,$zip)
    {
        $data = array(
'RELATIONSHIP'=> $relationship,
'APPID'=> $appid,
'NAME'=> $name,
'PHONE'=> $phone,
'ADDRESS1'=> $address1,
'ADDRESS2'=> $address2,
'CITY'=> $city,
'STATE'=> $state,
'ZIP'=> $zip,
        );
        $this->update($data, 'REFID = '. (int)$refid);
    }

    public function deleteApplicationReference($refid)
    {
        $this->delete('REFID =' . (int)$refid);
    }





}



